
def cmd():
    import os, sys, django
    sys.path.insert(0, '../')
    os.environ['DJANGO_SETTINGS_MODULE'] = 'server.settings'
    django.setup()
    from apps.models import machine, Command, order
    import time, os
    import paramiko
    ret = order.objects.all().order_by("-timedate")
    timestart = time.strftime('%H', time.localtime(time.time()))
    for data in ret:
        if int(data.timestart) == int(timestart) or data.timestart == "all":
            if data.relevancecmdid == None or data.relevancecmdid == "":
                pass
            else:
                if data.relevance != "all":
                    cmd_obj = Command.objects.get(id=data.relevancecmdid)
                    machine_obj = machine.objects.get(id=data.relevanceid)
                elif data.relevance == "all":
                    cmd_obj = Command.objects.get(id=data.relevancecmdid)
                    machine_obj = machine.objects.all().order_by("-timedate")
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                for machine_obj in machine_obj:
                    ssh.connect(hostname=machine_obj.ip, port=machine_obj.post, username=machine_obj.user, password=machine_obj.pwd)
                    for cmd in [cmd_obj.cmd1, cmd_obj.cmd2, cmd_obj.cmd3, cmd_obj.cmd4, cmd_obj.cmd5, cmd_obj.cmd6, cmd_obj.cmd7, cmd_obj.cmd8, cmd_obj.cmd9]:
                        if cmd == None or cmd == "":
                            continue
                        stdin, stdout, stderr = ssh.exec_command(cmd, get_pty=True)
                        res, err = stdout.read(), stderr.read()
                        if err:
                            print(err.decode())
                        print(res.decode())
                    ssh.close()
        if int(data.timestart) == int(timestart) or data.timestart == "all":
            if data.relevancecmdid1 == None or data.relevancecmdid1 == "":
                pass
            else:
                if data.relevance != "all":
                    cmd_obj = Command.objects.get(id=data.relevancecmdid)
                    machine_obj = machine.objects.get(id=data.relevanceid)
                elif data.relevance == "all":
                    cmd_obj = Command.objects.get(id=data.relevancecmdid)
                    machine_obj = machine.objects.all().order_by("-timedate")
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                for machine_obj in machine_obj:
                    ssh.connect(hostname=machine_obj.ip, port=machine_obj.post, username=machine_obj.user,
                                password=machine_obj.pwd)
                    for cmd in [cmd_obj.cmd1, cmd_obj.cmd2, cmd_obj.cmd3, cmd_obj.cmd4, cmd_obj.cmd5, cmd_obj.cmd6,
                                cmd_obj.cmd7, cmd_obj.cmd8, cmd_obj.cmd9]:
                        if cmd == None or cmd == "":
                            continue
                        stdin, stdout, stderr = ssh.exec_command(cmd, get_pty=True)
                        res, err = stdout.read(), stderr.read()
                        if err:
                            print(err.decode())
                        print(res.decode())
                    ssh.close()



def ping():
    import os, sys, django
    sys.path.insert(0, '../')
    os.environ['DJANGO_SETTINGS_MODULE'] = 'server.settings'
    django.setup()
    from apps.models import machine
    import time, os
    import paramiko
    for i in machine.objects.all():
        a = os.popen("ping -c 1  -w 1 " + i.ip1 + "|grep -w ttl|wc -l").read()
        if int(a) == 0:
            i.state = "关机"
        else:
            i.state = "运行中"
        i.save()











